package com.peternguyen.totolistpro.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.peternguyen.totolistpro.data.Configapp;
import com.peternguyen.totolistpro.sql.TodoSQLiteHelper;

public class ConfigDAO {
	private SQLiteDatabase db;
	private TodoSQLiteHelper dbHelper;

	public ConfigDAO(Context context) {
		dbHelper = new TodoSQLiteHelper(context);
		db = dbHelper.getWritableDatabase();
	}

	// Close the db
	public void close() {
		db.close();
	}

	// Create
	public void Insert(Configapp con) {
		ContentValues contentValues = new ContentValues();
		contentValues.put("name", con.get_name());
		contentValues.put("value", con.get_value());
		// Insert into DB
		db.insert("tblConfig", null, contentValues);
	}

	// update
	public void Update(Configapp con) {
		ContentValues contentValues = new ContentValues();
		contentValues.put("value", con.get_value());
		// Update datatbase
		Log.v("Name", con.get_name());
		Log.v("ID", String.valueOf(con.get_id()));
		db.update("tblConfig", contentValues, "_id= " + con.get_id(), null);
	}

	// Get Acc

	public Configapp getConfig(String name) {
		// Name of the columns we want to select
		String[] tableColumns = new String[] { "_id", "name", "value" };

		// Query the database
		Cursor cursor = db.query("tblConfig", tableColumns, "name='" + name
				+ "'", null, null, null, null);
		cursor.moveToFirst();
		Configapp con = new Configapp();
		try {
			con.set_id(cursor.getInt(0));
			con.set_name(cursor.getString(1));
			con.set_value(cursor.getString(2));
		} catch (Exception e) {
			return null;
		}
		return con;
	}

}
